<?php
 include_once __DIR__.'/ShippingMgr.class.php';	
	class OrderMgr
	{
		private $dbc;
		private $shippingMgr;
		
		 public function __construct($dbc = null)
	     {
	            if (isset($dbc))
	            {
	                $this->dbc = $dbc;
	                $this->shippingMgr=new ShippingMgr($dbc);
	            }
	     }
	     
	     public function addOrder($uid,$cid,$ship=null,$cc=null) 
	     {
	     	$order=R::dispense('isorder');
                $order->order_date=  date('Y-m-d H:i:s');
                $order->status=0;
                $order->customer_id = $cid;
                $oid=R::store($order);
                $order=R::load('isorder',$oid);
                
                
                $baskets=R::find('basket','user_session_id = :uid',array(':uid'=>$uid));
                
                foreach ($baskets as $key => $basket) {
                    $product=$basket->product;
                    $orderpli=R::dispense('orderpli');
                    $orderpli->product_id=$product->id;
                    $orderpli->qty=$basket->quantity;
                    $orderpli->sku=$product->sku;
                    $orderpli->name=$product->name;
                    $orderpli->color=$product->color;
                    $orderpli->size=$product->size;
                    $orderpli->weight=$product->weight;
                    $orderpli->packing=$product->packing;
                    $orderpli->isorder_id=$order->id; 
                    $sale = R::findOne('sales',' product_id = ? '
                            . 'AND ('
                            . '(NOW() BETWEEN sales.start_date AND sales.end_date) '
                            . 'OR '
                            . '(NOW() > sales.start_date AND sales.end_date IS NULL)'
                            . ')',array($product->id));
                    if(isset($sale))
                    {                     
                        $orderpli->sales_id=$sale->id;   
                        $orderpli->price_per=$sale->price;
                    }
                    else
                    {
                        $price=$product->withCondition('type= ?',array('SalePrice'))->ownProductprice;
                        foreach ($price as $key => $value) {
                            $orderpli->price_per=$value->price;
                        }
                    }
                    $orderpli->total=$orderpli->qty*$orderpli->price_per;
                    
                    R::store($orderpli);
                }
                	
                $q="SELECT SUM(total) FROM orderpli WHERE isorder_id=$oid";
                $r = mysqli_query($this->dbc, $q);		
	      	$num =mysqli_num_rows($r);
			 if($num == 1) 
			 {
			 	$row = mysqli_fetch_array($r,MYSQLI_NUM);	
			 	$subtotal=$row[0];
			 	$ship=$this->shippingMgr->getShipping($subtotal);
			 	$total=$subtotal + $ship;
			 	$q="UPDATE isorder SET total = $total , shipping = $ship WHERE id=$oid;";
				$r = mysqli_query($this->dbc, $q);	
				// For debugging purposes:
				//if (!$r) echo mysqli_error($dbc);
			 	
			 }	
	     	return array($oid,$total);
	     }
	     
	     public function getOrderContents($oid)
	     {	     	
			$items =array();
	     	$q="SELECT 
	     		oc.qty, oc.price_per, (oc.qty*oc.price_per) AS subtotal, ncp.name, o.total, o.shipping 
	     			FROM orderpli AS oc INNER JOIN product AS ncp ON oc.product_id=ncp.id 
	     				INNER JOIN isorder AS o ON oc.isorder_id=o.id WHERE oc.isorder_id=$oid";
	     	$r = mysqli_query($this->dbc, $q);
	     	// For debugging purposes:
			//if (!$r) echo mysqli_error($dbc);
			$num =mysqli_num_rows($r);
			if($num >0)
			{
				while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) 
				{
					 $_entry=array(); 
					 $_entry['quantity']= $row['qty'] ;
					 $_entry['price_per']= $row['price_per'] ;
					 $_entry['subtotal']= $row['subtotal'] ;
					 $_entry['name']= $row['name'] ;
					 $_entry['shipping']= $row['shipping'] ;
					 $_entry['total']= $row['total'] ;
					 $items[]=$_entry;
				}
			
			} 
			 return $items;
	     }
	}   